package com.link.base.base.performance.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.performance.dao.mybatis.mapper.BonusMapper;
import com.link.base.base.performance.model.Bonus;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 年度奖金
 *
 * @author 王昊
 * @date 2021-04-08 11:34:34
 */
@Service
public class BonusServiceImpl extends BasicServiceImpl<Bonus> implements BonusService {

    @Resource
    private BonusMapper    bonusMapper;

    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<Bonus> getBasicMapper() {
        return bonusMapper;
    }

    /**
     * 年度奖金审批
     * @param entity
     * @throws Exception
     */
    @Override
    public void bonusSubmit(Bonus entity) throws Exception {
        //根据entity查询对应年度奖金记录
        Bonus bonus = bonusMapper.queryById(entity);
        //定义审批流
        String approvalType = "bonusSubmit";

        ObjectMapper mapper = new ObjectMapper();
        String approvalDetail = mapper.writeValueAsString(bonus);
        //创建审批流
        flowService.createFlow(bonus.getId(), approvalType, null, bonus.getBusinessManager(),bonus.getId(), approvalDetail);
        //提交审批后，审批状态为【审批中】
        bonus.setApprovalStatus("InApproval");
        bonusMapper.update(bonus);
    }

    /**
     * 审批拒绝与通过方法
     * @param flow
     * @param arg1
     * @throws Exception
     */
    @Override
    public void apprBonusSubmit(Flow flow, String arg1, String arg2, String arg3, String arg4, String arg5) throws Exception {
        Long bounsId = flow.getFlowObjId();
        Bonus bonus = new Bonus();
        bonus.setId(bounsId);
        Bonus bonus1 = bonusMapper.queryById(bonus);
        bonus1.setApprovalStatus(arg1);
        bonusMapper.update(bonus1);
    }
}